package com.aiwiown.face.internal.util;

import com.aiwiown.face.ApiObject;
import com.aiwiown.face.domain.params.ApiEnumParam;
import com.aiwiown.face.domain.params.ApiParamType;
import com.aiwiown.face.internal.mapping.ApiField;

import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName : RequestParamUtils
 * @Description :
 * @Author : dbin0123
 * @Date: 2020-03-14 18:26
 */
public class RequestParamUtils {

    public static Map<String, String> getTextParams(ApiObject bizModel) throws IllegalAccessException {
        Map<String, String> param = new HashMap<>();
        if (null != bizModel) {
            Field[] declaredFields = bizModel.getClass().getDeclaredFields();
            if (null != declaredFields && declaredFields.length > 0) {
                for (Field field : declaredFields) {
                    ApiField apiField = field.getAnnotation(ApiField.class);
                    if (null != apiField && apiField.paramType() != ApiParamType.FILE) {
                        //打开私有访问
                        field.setAccessible(true);
                        Object o = field.get(bizModel);
                        if (Objects.nonNull(o)) {
                            if (o instanceof String) {
                                param.put(apiField.value(), String.valueOf(o));
                            } else if (o instanceof ApiEnumParam) {
                                param.put(apiField.value(), ((ApiEnumParam) o).getParamValue());
                            } else if (o instanceof Integer) {
                                param.put(apiField.value(), String.valueOf(o));
                            }
                        }
                    }
                }
            }
        }
        return param;
    }

    public static Map<String, byte[]> getByteArrayParams(ApiObject bizModel) throws IllegalAccessException {
        Map<String, byte[]> param = new HashMap<>();
        if (null != bizModel) {
            Field[] declaredFields = bizModel.getClass().getDeclaredFields();
            if (null != declaredFields && declaredFields.length > 0) {
                for (Field field : declaredFields) {
                    ApiField apiField = field.getAnnotation(ApiField.class);
                    if (Objects.nonNull(apiField) && apiField.paramType() == ApiParamType.FILE) {
                        //打开私有访问
                        field.setAccessible(true);
                        Object o = field.get(bizModel);
                        if (Objects.nonNull(o)) {
                            if (o instanceof File) {
                                //文件
                                param.put(apiField.value(), StreamUtil.file2byte((File) o));
                            } else if (o instanceof String) {
                                //base64串(可以使用字符串传递)
                                String val = (String) o;
                                if (val.startsWith("http")) {
                                    param.put(apiField.value(), StreamUtil.decode(StreamUtil.imgUrlToBase64Str(val)));
                                } else {
                                    param.put(apiField.value(), StreamUtil.decode(val));
                                }
                            } else if (o instanceof byte[] || o instanceof Byte[]) {
                                //byte数组
                                param.put(apiField.value(), (byte[]) o);
                            }
                        }
                    }
                }
            }
        }
        return param;
    }


}
